package 蓝桥杯算法;

import 链表.Node1;

import java.util.function.Consumer;

public class day04 {
    public static void main(String[] args) {
        day04 aa=new day04();
        LinkedList list=new LinkedList();
        list.addNode(1);
        list.addNode(2);
        list.addNode(3);
        list.addNode(4);
        Node head=list.head;
        aa.spairpower1(head.next);
        aa.loop2(head);
    }
    public  void loop2(Node head){//利用接口实现方法的多种使用
        Node p=head.next;
        while (p!=null){
            System.out.println(p.value);
            p=p.next;
        }
    }

    static class Node{//创建链表
        int value;
        Node next;
        public Node(int value){
            this.next=next;
            this.value=value;
        }
    }
    static class LinkedList { //创建链表
        int size;
        Node head;

        public LinkedList() {
            this.size = 0;
            this.head = new Node(0);
        }
        public void addNode(int value){
            Node newNode=new Node(value);
            if(head==null){
                head=newNode;
            }
            else {
                Node current=head;
                while(current.next!=null){
                    current=current.next;
                }
                current.next=newNode;
            }
        }
    }

    public Node reverse(Node head){//双指针法
        Node pre=null;
        Node cur=head;
        Node flag=null;
        while(cur!=null){
           flag=cur.next;//保留cur的下一个位置
           cur.next=pre;
           pre=cur;
           cur=flag;

        }
        return pre;
    }
    public void spairpower(Node head) {//循环
        Node cur=head;
        while(cur.next!=null&&cur.next.next!=null){
            Node temp1=cur.next;
            Node temp2=cur.next.next.next;
            cur.next=cur.next.next;
            cur.next.next=temp1;
            temp1.next.next.next=temp2;
            cur=cur.next.next;//新的节点
        }
    }
    public Node spairpower1(Node head){
       if(head==null||head.next==null){
           return head;
       }
       Node next=head.next;
       Node newNode=spairpower1(next.next);
       next.next=head;
       head.next=newNode;
       return next;
    }
}
